package cn.ffcs.uec.common.component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Treeitem;

import cn.ffcs.uec.common.vo.UecBdDefdoc;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.zkplus.zul.tree.node.impl.TreeNodeImpl;
import cn.ffcs.uom.staffrole.constants.StaffRoleConstants;
import lombok.Getter;
import lombok.Setter;

public class UecBdDefdocTreeBandboxExt extends Bandbox implements IdSpace {

    private static final long serialVersionUID = 3516404716600574912L;

    private final String zul = "/pages_nmg/uec_common/comp/uec_bd_defdoc_tree_bandbox_ext.zul";

    /**
     * 字典类
     */
    @Getter
    private UecBdDefdoc uecBdDefdoc;

    /**
     * 字典表树
     */
    @Getter
    @Setter
    private UecBdDefdocTree uecBdDefdocTree ;

    @Getter
    @Setter
    private String mode;// 单选和多选两种模式

    /**
     * 限制树根节点展示值
     */
    @Getter
    @Setter
    private String flag;

    @Getter
    @Setter
    private String range;// 只选择末级节点和任意选择节点两种模式

    @Getter
    private List<UecBdDefdoc> uecBdDefdocs;

    private List<UecBdDefdoc> resultArr;// 选择节点列表,left=节点标识,right=节点中文名

    @Getter
    @Setter
    private String resultText;// 返回文本

    public UecBdDefdocTreeBandboxExt() {
        Executions.createComponents(this.zul, this, null);
        Components.wireVariables(this, this);
        Components.addForwards(this, this, '$');
        uecBdDefdocTree.setUecBdDefdocTreeBandboxExt(this);
    }

    /**
     * 初始化
     */
    @SuppressWarnings("unused")
    public void onCreate() {
        this.setReadonly(true);
        if ("multipleSelection".equals(mode)) {
            uecBdDefdocTree.setMultiple(true);
            //uecBdDefdocTree.setCheckmark(true);
        } else {
            uecBdDefdocTree.setMultiple(false);
           // uecBdDefdocTree.setCheckmark(false);
        }
        if (!StrUtil.isNullOrEmpty(flag)) {
            uecBdDefdocTree.setFlag(this.getFlag());
        }
      //显示勾选框
        uecBdDefdocTree.setCheckmark(true);
        @SuppressWarnings("unchecked")
        Set<Treeitem> set = this.uecBdDefdocTree.getSelectedItems();
        // ..实现初始化加载已选信息
        // TODO 加了监听box就会多出下面的空白
        // 写死高度像素值暂时解决
        this.addEventListener(Events.ON_OPEN, new EventListener() {

            @SuppressWarnings({ "unchecked", "rawtypes" })
            @Override
            public void onEvent(Event event) throws Exception {
                // set initial check state
                Collection<Treeitem> tis = uecBdDefdocTree.getItems();
                for (Treeitem ti : tis) {
                    UecBdDefdoc uecBdDefdoc = (UecBdDefdoc) ((TreeNodeImpl) ti.getValue())
                            .getEntity();
                }
                if (resultArr != null) {
                    for (Treeitem ti : tis) {
                        UecBdDefdoc uecBdDefdoc = (UecBdDefdoc) ((TreeNodeImpl) ti.getValue())
                                .getEntity();
                        if (resultArr.contains(uecBdDefdoc)) {
                            ti.setSelected(true);
                        } else {
                            ti.setSelected(false);
                        }
                    }
                }
            }
        });
    }

    public void setUecBdDefdocs(List<UecBdDefdoc> uecBdDefdocs) {
        String uecBdDefdocName = "";
        if (uecBdDefdocs != null && uecBdDefdocs.size() > 0) {
            for (int i = 0; i < uecBdDefdocs.size(); i++) {
                if (i == uecBdDefdocs.size() - 1) {
                    uecBdDefdocName += uecBdDefdocs.get(i).getName();
                } else {
                    uecBdDefdocName += uecBdDefdocs.get(i).getName() + ",";
                }
            }
        }
        this.setValue(uecBdDefdocName);
        this.uecBdDefdocs = uecBdDefdocs;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void onClick$okButton() {
        Set<Treeitem> itemsSet = this.uecBdDefdocTree.getSelectedItems();
        List<UecBdDefdoc> list = new ArrayList<UecBdDefdoc>();
        if (itemsSet != null) {
            Iterator<Treeitem> it = itemsSet.iterator();
            while (it.hasNext()) {
                Treeitem ti = it.next();
                if (ti.getTreechildren() != null) {
                    // 不是末级节点
                    continue;
                }
                UecBdDefdoc sl = (UecBdDefdoc) ((TreeNodeImpl) ti.getValue()).getEntity();
                if (sl != null && !StaffRoleConstants.IS_PARENT.equals(sl.getPid())) {
                    list.add(sl);
                }
            }
        }
        this.setUecBdDefdocs(list);
        this.close();
    }

    public void onClick$cancelButton() {
        this.close();
    }

    public List<UecBdDefdoc> getResultArr() {
        return resultArr;
    }

    public void setResultArr(List<UecBdDefdoc> resultArr) {
        this.resultArr = resultArr;
    }

    public void setUecBdDefdoc(UecBdDefdoc uecBdDefdoc) {
        this.setValue(uecBdDefdoc == null ? "" : uecBdDefdoc.getName());
        this.uecBdDefdoc = uecBdDefdoc;
        Events.postEvent("onGetBdDefDoc", this, uecBdDefdoc);
    }

    /**
     * 快速设置初始值
     * 
     * @author 曾臻
     * @date 2013-6-19
     * @param codeList
     *            编码列表
     */
    public void setInitialValue(List<UecBdDefdoc> uecBdDefdocs) {
        String str = "";
        resultArr = new ArrayList<UecBdDefdoc>();
        if (uecBdDefdocs != null && uecBdDefdocs.size() > 0) {
            for (UecBdDefdoc uecBdDefdoc : uecBdDefdocs) {
                str += uecBdDefdoc.getName() + ",";
                resultArr.add(uecBdDefdoc);
            }
        }
        resultText = StringUtils.removeEnd(str, ",");
        this.setText(resultText);
    }
  
}
